package de.ohmhochschule.bme;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class FileIOActivity extends Activity {
	private PaintView paintView;
	private ArrayList<Float> points;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);

        points = new ArrayList<Float>();
		paintView = new PaintView(this);
		setContentView(paintView);
		paintView.requestFocus();
    }

	public ArrayList<Float> getPoints() {
		return points;
	}
	
	public void addLineToPath(float x, float y) {
		points.add(x);
		points.add(y);
	}

	@Override
	protected void onPause() {		
		try {
			FileOutputStream fos = openFileOutput("points.ser", MODE_PRIVATE);
			//FileOutputStream fos = new FileOutputStream("/sdcard/points1.ser");			
			//fos.write( 42 );
			ObjectOutputStream oos = new ObjectOutputStream( fos );
			oos.writeObject(points);
			oos.close();
			fos.close();
		} catch (Exception e) {
			Log.e(getLocalClassName(), e.getMessage());
		}

		super.onPause();
	}

	@Override
	protected void onResume() {
		try {
			FileInputStream fis = openFileInput("points.ser");
			//FileInputStream fis = new FileInputStream("/sdcard/points1.ser");
			//int i = fis.read();
			//Log.i(getLocalClassName(), "i="+i);
			ObjectInputStream ois = new ObjectInputStream(fis);			
			Object o = ois.readObject();
						
			if ( o instanceof ArrayList<?>) {
				points = (ArrayList<Float>) o;
			} else {
				Log.e(getLocalClassName(), "we should never get here... ");				
			}						
			
			ois.close();
			fis.close();
		} catch (Exception e) {
			Log.e(getLocalClassName(), e.getMessage());
		}

		super.onResume();
	}
	
	
	
}